package com.qf.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsOrderMapper;
import com.qf.pojo.DtsOrder;
import com.qf.vo.CatVo;
import com.qf.vo.CategorySellAmts;
import com.qf.vo.DayStatis;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author mkr
 * @version V1.0
 * @Project java2203-mkr
 * @Package com.qf.service
 * @Description:
 * @Date 2022/6/17 9:34
 */
@Service
public class OrderServiceImpl implements  OrderService {

    @Autowired
    private DtsOrderMapper orderMapper;


    @Override
    public int countOrder() {
        QueryWrapper<DtsOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", false);
        Integer count = orderMapper.selectCount(queryWrapper);
        return count;
    }

    @Override
    public List<DayStatis> incrCount() {
        List<DayStatis> list = orderMapper.staticIncresOrderCnt();
        return list;
    }

    @Override
    public List<CategorySellAmts> categoryCount() {
        List<CategorySellAmts> list = orderMapper.categorySell();
        return list;
    }

    /**
     * 分页查询
     * @param userId  根据用户Id查询
     * @param orderSn  根据订单编号查询
     * @param orderStatus 根据订单状态查询
     * @param page  当前页。默认1
     * @param limit  每页显示条数，默认20条
     * @param sort  排序字段名，默认按照add_time添加时间排序
     * @param order  排序方式，，默认desc降序
     * @return
     */
    @Override
    public IPage<DtsOrder> queryOrderPage(Integer userId, String orderSn, Integer orderStatus, Integer page, Integer limit, String sort, String order) {

       //1：创建分页对象
        Page page1 = new Page(page, limit);

        //2：封装查询对象
        QueryWrapper<DtsOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", false);

        //判断查询条件是否为空
        if (userId != null){
            queryWrapper.eq("userId", userId);
        }if (orderSn != null){
            queryWrapper.eq("orderSn", orderSn);
        }if ( orderStatus != null){
            queryWrapper.eq(" orderStatus",  orderStatus);
        }


        //判断排序字段名，排序方式是否为空
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)){
            if (order.equalsIgnoreCase("asc")){
                queryWrapper.orderByAsc(sort);
            }else {
                queryWrapper.orderByDesc(sort);
            }
        }

        //3:查询并返回

       IPage<DtsOrder> resultPage = orderMapper.selectPage(page1, queryWrapper);
        return resultPage;
    }

    @Override
    public int add(DtsOrder order) {
        return 0;
    }


//    @Override
//    public List<DtsOrder> findShipChannel() {
//        List<DtsOrder> findshipChannel = orderMapper.findshipChannel();
//        return findshipChannel;


//    }
}
